﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService

Partial Class Control_MSDS_Section09
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Label
        Translate()
        If Not IsPostBack Then
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "duyjs", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "duyjs", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If

            hidMSDSID.Value = IIf(IsNothing(Request.QueryString("Id")), 0, CInt(Request.QueryString("Id")))
            ShowData()
        End If
    End Sub
    Protected Sub Translate()
        'lblSection09Header.Text = getTranslate("lblSection09Header", Session("LanguageId"), lblSection09Header.Text)
        'lblS911.Text = getTranslate("lblS911", Session("LanguageId"), lblS911.Text)
        'lblS921.Text = getTranslate("lblS921", Session("LanguageId"), lblS921.Text)
        'lblS931.Text = getTranslate("lblS931", Session("LanguageId"), lblS931.Text)
        'lblS941.Text = getTranslate("lblS941", Session("LanguageId"), lblS941.Text)
        'lblS951.Text = getTranslate("lblS951", Session("LanguageId"), lblS951.Text)
        'lblS961.Text = getTranslate("lblS961", Session("LanguageId"), lblS961.Text)
        'lblS971.Text = getTranslate("lblS971", Session("LanguageId"), lblS971.Text)
        'lblS981.Text = getTranslate("lblS981", Session("LanguageId"), lblS981.Text)

        'lblS991.Text = getTranslate("lblS991", Session("LanguageId"), lblS991.Text)
        'lblS9101.Text = getTranslate("lblS9101", Session("LanguageId"), lblS9101.Text)
        'lblS9111.Text = getTranslate("lblS9111", Session("LanguageId"), lblS9111.Text)
        'lblS9121.Text = getTranslate("lblS9121", Session("LanguageId"), lblS9121.Text)
        'lblS9131.Text = getTranslate("lblS9131", Session("LanguageId"), lblS9131.Text)
        'lblS9141.Text = getTranslate("lblS9141", Session("LanguageId"), lblS9141.Text)
        'lblS9151.Text = getTranslate("lblS9151", Session("LanguageId"), lblS9151.Text)
        'lblS9161.Text = getTranslate("lblS9161", Session("LanguageId"), lblS9161.Text)
    End Sub
    Protected Sub ShowData()
        Using data As New VSEntities
            'Tiêu đề doanh nghiệp
            Dim lang = CInt(Session("LanguageId"))
            Dim Id As Integer = hidMSDSID.Value
            Dim p As MSDSSection09 = (From a In data.MSDSSection09 Where a.MSDSId = Id And a.LanguageId = lang).SingleOrDefault
            If Not IsNothing(p) Then
                hidID.Value = p.Section09Id

                'Content
                txtS911.Text = IIf(IsNothing(p.S911) = True, "", p.S911)
                txtS921.Text = IIf(IsNothing(p.S921) = True, "", p.S921)
                txtS931.Text = IIf(IsNothing(p.S931) = True, "", p.S931)
                txtS941.Text = IIf(IsNothing(p.S941) = True, "", p.S941)
                txtS951.Text = IIf(IsNothing(p.S951) = True, "", p.S951)
                txtS961.Text = IIf(IsNothing(p.S961) = True, "", p.S961)
                txtS971.Text = IIf(IsNothing(p.S971) = True, "", p.S971)
                txtS981.Text = IIf(IsNothing(p.S981) = True, "", p.S981)
                txtS991.Text = IIf(IsNothing(p.S991) = True, "", p.S991)
                txtS9101.Text = IIf(IsNothing(p.S9101) = True, "", p.S9101)
                txtS9111.Text = IIf(IsNothing(p.S9111) = True, "", p.S9111)
                txtS9121.Text = IIf(IsNothing(p.S9121) = True, "", p.S9121)
                txtS9131.Text = IIf(IsNothing(p.S9131) = True, "", p.S9131)
                txtS9141.Text = IIf(IsNothing(p.S9141) = True, "", p.S9141)
                txtS9151.Text = IIf(IsNothing(p.S9151) = True, "", p.S9151)
                txtS9161.Text = IIf(IsNothing(p.S9161) = True, "", p.S9161)
            End If
          
        End Using
    End Sub
#End Region
#Region "Event for control "
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Using data As New VSEntities
            Try
                Dim p As MSDSSection09
                p = (From q In data.MSDSSection09
                Where q.Section09Id = hidID.Value
                  ).FirstOrDefault()
                Dim _isNew = True
                If Not p Is Nothing Then
                    _isNew = False
                Else
                    p = New MSDSSection09
                End If
                With p
                    .S911 = txtS911.Text.Trim()
                    .S921 = txtS921.Text.Trim()
                    .S931 = txtS931.Text.Trim()
                    .S941 = txtS941.Text.Trim()
                    .S951 = txtS951.Text.Trim()
                    .S961 = txtS961.Text.Trim()
                    .S971 = txtS971.Text.Trim()
                    .S981 = txtS981.Text.Trim()
                    .S991 = txtS991.Text.Trim()
                    .S9101 = txtS9101.Text.Trim()
                    .S9111 = txtS9111.Text.Trim()
                    .S9121 = txtS9121.Text.Trim()
                    .S9131 = txtS9131.Text.Trim()
                    .S9141 = txtS9141.Text.Trim()
                    .S9151 = txtS9151.Text.Trim()
                    .S9161 = txtS9161.Text.Trim()
                    .LanguageId = Session("LanguageId")
                    .MSDSId = hidMSDSID.Value
                End With
                If _isNew Then
                    p.Created = Now
                    p.Creator = Session("UserName")
                    data.MSDSSection09.AddObject(p)
                Else
                    p.Modified = Now
                    p.Modifier = Session("UserName")
                End If
                data.SaveChanges()

                Insert_App_Log(If(_isNew, "Insert", "Update") & "  MSDS Section09:" & hidMSDSID.Value & "", Function_Name.MSDSOriginal, If(_isNew, Audit_Type.Create, Audit_Type.Edit), Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                Excute_Javascript("AlertboxRedirect('Lưu dữ liệu thành công.','../../Page/MSDS/Section10.aspx?Id=" & hidMSDSID.Value & "');", Me.Page, True)
            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
            Finally
                GC.Collect()
            End Try
        End Using
    End Sub
    Protected Sub btnPrevious_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
        Response.Redirect("../../Page/MSDS/Section08.aspx?Id=" & hidMSDSID.Value)
    End Sub
#End Region

End Class
